package com.kaili.common.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Created by renzhikun on 18-5-11.
 */
public class PinYinUtil {
    private static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    public static enum Type {
        UPPERCASE,
        LOWERCASE,
        FIRSTUPPER
    }

    private PinYinUtil() {
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    /**
     * 汉字转拼音（默认小写）
     * @param str
     * @return
     */
    public static String toPinYin(String str) {
        try {
            return toPinYin(str, "", Type.LOWERCASE);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            return "";
        }
    }

    /**
     * 汉字转拼音首字母（默认小写）
     * @param str
     * @return
     */
    public static String toSimplePinYin(String str) {
        try {
            return toSimplePinYin(str, "", Type.LOWERCASE);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            return "";
        }
    }

    public static String toPinYin(String str, String spera)  {
        try {
            return toPinYin(str, spera, Type.LOWERCASE);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            return "";
        }
    }

    public static String toSimplePinYin(String str, String spera)  {
        try {
            return toSimplePinYin(str, spera, Type.LOWERCASE);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            return "";
        }
    }

    public static String toSimplePinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
        if (str == null || str.trim().length() == 0) {
            return "";
        }
        new PinYinUtil();
        if (type == Type.LOWERCASE) {
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        } else {
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        }
        String py = "";
        String temp;
        String[] t;
        for (int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            if((int)c <= 128) {
                py += c;
            } else {
                t = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if (t == null) {
                    py += c;
                } else {
                    temp = t[0].substring(0,1);
                    if (type == Type.FIRSTUPPER)
                        temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
                    py += temp+(i==str.length()-1?"":spera);
                }
            }
        }
        return py.trim();
    }

    public static String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
        if (str == null || str.trim().length() == 0) {
            return "";
        }
        new PinYinUtil();
        if (type == Type.LOWERCASE) {
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        } else {
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        }
        String py = "";
        String temp;
        String[] t;
        for (int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            if((int)c <= 128) {
                py += c;
            } else {
                t = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if (t == null) {
                    py += c;
                } else {
                    temp = t[0];
                    if (type == Type.FIRSTUPPER)
                        temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
                    py += temp+(i==str.length()-1?"":spera);
                }
            }
        }
        return py.trim();
    }

}
